//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;

namespace OpenLS.UI
{
    public class AutoCollapsingPanel : Panel
    {



        public static bool GetOverflows(DependencyObject obj)
        {
            return (bool)obj.GetValue(OverflowsProperty);
        }

        public static void SetOverflows(DependencyObject obj, bool value)
        {
            obj.SetValue(OverflowsProperty, value);
        }

        // Using a DependencyProperty as the backing store for Overflows.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty OverflowsProperty =
            DependencyProperty.RegisterAttached("Overflows", typeof(bool), typeof(AutoCollapsingPanel), new UIPropertyMetadata(false));


        public ControlTemplate NarrowTemplate
        {
            get { return (ControlTemplate)GetValue(NarrowTemplateProperty); }
            set { SetValue(NarrowTemplateProperty, value); }
        }

        // Using a DependencyProperty as the backing store for NarrowTemplate.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty NarrowTemplateProperty =
            DependencyProperty.Register("NarrowTemplate", typeof(ControlTemplate), typeof(AutoCollapsingPanel), new UIPropertyMetadata(null));


        public ControlTemplate WideTemplate
        {
            get { return (ControlTemplate)GetValue(WideTemplateProperty); }
            set { SetValue(WideTemplateProperty, value); }
        }

        // Using a DependencyProperty as the backing store for WideTemplate.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty WideTemplateProperty =
            DependencyProperty.Register("WideTemplate", typeof(ControlTemplate), typeof(AutoCollapsingPanel), new UIPropertyMetadata(null));

        protected override Size ArrangeOverride(Size finalSize)
        {
          //\\  Debug.WriteLine("ArrangeOverride " + GetType());
            Child.Arrange(new Rect(new Point(0, 0), finalSize));
            return new Size(Child.ActualWidth, Child.ActualHeight);
        }
        protected override Size MeasureOverride(Size availableSize)
        {
           //\\ Debug.WriteLine("MeasureOverride " + GetType());
            Child.Measure(availableSize);
            if (GrandChild == null)
                return Child.DesiredSize;
            if (Child.Template != NarrowTemplate)
            {
                GrandChild.Measure(new Size(availableSize.Width, availableSize.Height)); //\\removed + 20
                if (GetOverflows(GrandChild))//\\ GrandChild.DesiredSize.Width > availableSize.Width + 1)
                {
                    Child.Template = NarrowTemplate;
                    Debug.WriteLine("Switching to narrow template because desired size" + GrandChild.DesiredSize.Width + " is greater than " + availableSize.Width);
                    Child.Measure(availableSize);
                }
                GrandChild.Measure(new Size(availableSize.Width, availableSize.Height));
                return new Size(Math.Min(availableSize.Width, Child.DesiredSize.Width), Child.DesiredSize.Height);
            }
            else
            {
                GrandChild.InvalidateMeasure();
                GrandChild.Measure(new Size(availableSize.Width, availableSize.Height));
                GrandChild.InvalidateMeasure();
                if (!GetOverflows(GrandChild) || GrandChild.DesiredSize.Width <= availableSize.Width) //\\ GrandChild.DesiredSize.Width <= availableSize.Width)
                {
                    Child.Template = WideTemplate;
                    Debug.WriteLine("Switching to wide template");

                    Child.Measure(availableSize);
                }
                //\\if (Child.DesiredSize.Width < availableSize.Width)
                return new Size(Math.Min(availableSize.Width, GrandChild.DesiredSize.Width), GrandChild.DesiredSize.Height);
                //\\return Child.DesiredSize;


            }
        }
        ContentControl Child { get { return Children[0] as ContentControl; } }
        UIElement GrandChild { get { return Child.Content as UIElement; } }


    }
}
